﻿using DtRlDeviceApiNet20;
using DtRlDeviceApiNet20.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Web;

namespace App.Sdx.Api
{
    /// <summary>
    /// getplan 的摘要说明
    /// </summary>
    public class getplan : IHttpHandler
    {
        private ApiWebClientInterface _apiClientInterface = new ApiWebClientInterface();
        static string Url = ConfigurationManager.AppSettings["GetTrainInfo"].ToString();
        static string SimplingCode = ConfigurationManager.AppSettings["SimplingCode"].ToString();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try
            {
                GetTrainInfoPara getTrainInfoPara = new GetTrainInfoPara();
                getTrainInfoPara.SimplingCode = SimplingCode;
                TrainInfo[] trainInfo = _apiClientInterface.GetTrainInfo(Url, getTrainInfoPara);
                if (this._apiClientInterface.ErrorCode == 0)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    if (trainInfo != null)
                    {
                        if (trainInfo.Length > 0)
                        {
                            stringBuilder.Append("[");
                            for (int i = 0; i < trainInfo.Length; i++)
                            {
                                stringBuilder.Append("{");
                                stringBuilder.Append($"\"BatchInfoId\":\"{trainInfo[i].BatchInfoId}\",");
                                stringBuilder.Append($"\"CanSampling\":{trainInfo[i].CanSampling},");
                                stringBuilder.Append($"\"CarNo\":\"{trainInfo[i].CarNo}\",");
                                stringBuilder.Append($"\"CarType\":\"{trainInfo[i].CarType}\",");
                                stringBuilder.Append($"\"CreateTime\":\"{trainInfo[i].CreateTime}\",");
                                stringBuilder.Append($"\"FromIp\":\"{trainInfo[i].FromIp}\",");
                                stringBuilder.Append($"\"Id\":\"{trainInfo[i].Id}\",");
                                stringBuilder.Append($"\"IsSuccess\":{trainInfo[i].IsSuccess},");
                                stringBuilder.Append($"\"IsWeight\":{trainInfo[i].IsWeight},");
                                stringBuilder.Append($"\"OrgId\":\"{trainInfo[i].OrgId}\",");
                                stringBuilder.Append($"\"SetBucketNo\":\"{trainInfo[i].SetBucketNo}\",");
                                stringBuilder.Append($"\"SimplingCode\":\"{trainInfo[i].SimplingCode}\",");
                                stringBuilder.Append($"\"SimplingLocation\":\"{trainInfo[i].SimplingLocation}\",");
                                stringBuilder.Append($"\"SimplingPoint\":{trainInfo[i].SimplingPoint},");
                                stringBuilder.Append($"\"SimplingSource\":\"{trainInfo[i].SimplingSource}\",");
                                stringBuilder.Append($"\"SimplingWeight\":{trainInfo[i].SimplingWeight},");
                                stringBuilder.Append($"\"SupplierCode\":\"{trainInfo[i].SupplierCode}\",");
                                stringBuilder.Append($"\"SysId\":\"{trainInfo[i].SysId}\",");
                                stringBuilder.Append($"\"Token\":\"{trainInfo[i].Token}\",");
                                stringBuilder.Append($"\"TrainIndex\":\"{trainInfo[i].TrainIndex}\",");
                                stringBuilder.Append($"\"TrainSamplingCode\":\"{trainInfo[i].TrainSamplingCode}\",");
                                stringBuilder.Append($"\"TransLong\":{trainInfo[i].TransLong},");
                                stringBuilder.Append($"\"TransWide\":{trainInfo[i].TransWide},");
                                stringBuilder.Append($"\"TravelNo\":\"{trainInfo[i].TravelNo}\"");
                                if (i == trainInfo.Length - 1)
                                    stringBuilder.Append("}");
                                else
                                    stringBuilder.Append("},");
                            }
                            stringBuilder.Append("]");
                            //LogHelper.DoDataWrite(stringBuilder.ToString());
                            context.Response.Write(stringBuilder.ToString());
                        }
                        context.Response.Write(stringBuilder.ToString());
                    }
                    context.Response.Write(stringBuilder.ToString());
                }
                else
                {
                    LogHelper.DoErrorWrite($"ErrorText:{this._apiClientInterface.ErrorText}  ErrorCode:{this._apiClientInterface.ErrorCode}");
                    context.Response.Write($"ErrorText:{this._apiClientInterface.ErrorText}  ErrorCode:{this._apiClientInterface.ErrorCode}");
                    //return;
                }
            }
            catch (Exception ex)
            {
                LogHelper.DoErrorWrite("Error:" + ex.Message);
                LogHelper.DoErrorWrite("Error:" + ex.StackTrace);
                context.Response.Write("Error:" + ex.Message);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}